﻿Public Class FrmNhaCungCap
    Dim provider As New DataProvider
    Dim flag As String = 0
    Dim sql As String
    Private Sub FrmNhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
    End Sub
    Private Sub LoadDuLieu()
        Dim sql As String
        sql = "SELECT ncc.MaNCC, ncc.TenNCC, ncc.DiaChiNCC, ncc.DienThoaiNCC, ncc.FaxNCC, ncc.MSTNCC, nn.TenNhomNCC   FROM NhaCungCap ncc, NhomNCC nn WHERE ncc.MaNhomNCC = nn.MaNhomNCC"
        dtgnhacc.DataSource = provider.ExecuteQuery(sql)
        Enable(False)
        btnluu.Enabled = False
        btnhuy.Enabled = False
        btnthem.Enabled = True
        btnsua.Enabled = True
        cbxnhomncc.Enabled = False
        LoadNhomNCC()
    End Sub
    Private Sub LoadNhomNCC()

        sql = "select * from NhomNCC"
        cbxnhomncc.DataSource = provider.ExecuteQuery(sql)
        cbxnhomncc.ValueMember = "MaNCC"
        cbxnhomncc.DisplayMember = "TenNhomNCC"


    End Sub
    Private Sub Enable(ByVal ena As Boolean)
        txtdiachi.Enabled = ena
        txtmancc.Enabled = ena
        txtsdt.Enabled = ena
        txttenncc.Enabled = ena
        cbxnhomncc.Enabled = ena
        dtgnhacc.Enabled = Not ena
    End Sub

    Private Sub dtgnhacc_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgnhacc.CellClick
        Try
            Dim row As Integer
            Dim dt As DataTable
            row = dtgnhacc.CurrentCell.RowIndex
            txtmancc.Text = dtgnhacc.Rows(row).Cells(1).Value
            txttenncc.Text = dtgnhacc.Rows(row).Cells(2).Value
            txtsdt.Text = dtgnhacc.Rows(row).Cells(3).Value
            txtdiachi.Text = dtgnhacc.Rows(row).Cells(4).Value
            txtfaxncc.Text = dtgnhacc.Rows(row).Cells(5).Value
            txtmstncc.Text = dtgnhacc.Rows(row).Cells(6).Value
            cbxnhomncc.Text = dtgnhacc.Rows(row).Cells(0).Value
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        Enable(True)
        flag = 1
        btnluu.Enabled = True
        btnhuy.Enabled = True
        btnsua.Enabled = False

        Enable(True)

    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        If txtmancc.Text = "" Then
            MessageBox.Show("Chọn 1 nhà cung cấp để sửa")
        Else
            flag = 2
            btnluu.Enabled = True
            btnhuy.Enabled = True
            btnthem.Enabled = False
            cbxnhomncc.Enabled = True
            Enable(True)
        End If
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try
            If flag = 1 Then
                Dim mancc As String = txtmancc.Text
                Dim tenncc As string = txttenncc.Text
                Dim diachi As String = txtdiachi.Text
                Dim sdt As String = txtsdt.Text
                Dim faxncc As String = txtfaxncc.Text
                Dim mst As String = txtmstncc.Text
                Dim manhomncc As String
                manhomncc = cbxnhomncc.SelectedValue

                sql = "select * from NhaCungCap where mancc='" + mancc + "'"
                Dim dt As New DataTable
                dt = provider.ExecuteQuery(sql)

                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Mã nhà cung cấp đã tồn tại")
                    txtmancc.Focus()
                Else
                    sql = "INSERT INTO NhaCungCap(	MaNCC,	TenNCC,	DiaChiNCC,	DienThoaiNCC,FaxNCC,MaNhomNCC)"
                    sql += "VALUES('" + mancc + "',N'" + tenncc + "',N'" + diachi + "','" + sdt + "','" + faxncc + "','" + manhomncc + "')"
                    provider.ExecuteNonQuery(sql)
                    MessageBox.Show("Thêm nhà cung cấp thành công")

                End If

            ElseIf flag = 2 Then
                Dim mancc As String = txtmancc.Text
                Dim tenncc As String = txttenncc.Text
                Dim diachi As String = txtdiachi.Text
                Dim sdt As String = txtsdt.Text
                Dim faxncc As String = txtfaxncc.Text
                Dim mst As String = txtmstncc.Text
                Dim manhomncc As String
                manhomncc = cbxnhomncc.SelectedValue

                sql = "UPDATE NhaCungCap SET MaNCC = '" + mancc + "',TenNCC = N'" + tenncc + "',DiaChiNCC = N'" + diachi + "',"
                sql += "DienThoaiNCC = '" + sdt + "',FaxNCC = '" + faxncc + "',MSTNCC = '" + mst + "',	MaNhomNCC = '" + manhomncc + "'"
                provider.ExecuteNonQuery(sql)
                MessageBox.Show("Cập nhật nhà cung cấp thành công ")
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class